package com.xuecheng.content.application.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.application.service.TeachplanApplication;
import com.xuecheng.content.application.vo.req.CourseAssociationMediaCmd;
import com.xuecheng.content.application.vo.req.TeachplanCreateCmd;
import com.xuecheng.content.domain.model.dto.TeachplanDto;
import com.xuecheng.content.domain.model.entity.Teachplan;
import com.xuecheng.content.domain.model.entity.TeachplanMedia;
import com.xuecheng.content.domain.service.TeachplanMediaService;
import com.xuecheng.content.domain.service.TeachplanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-08-03  16:14
 */
@Service
public class TeachplanApplicationImpl implements TeachplanApplication {

    @Autowired
    private TeachplanService teachplanService;

    @Autowired
    private TeachplanMediaService teachplanMediaService;

    @Override
    public List<TeachplanDto> queryTreeNodes(Long courseId) {
        return teachplanService.queryTreeNodes(courseId);
    }

    @Override
    public void saveOrUpdateTeachplan(TeachplanCreateCmd teachplanCreateCmd) {
        // todo 看后续这里的参数是否需要变，若不变就缩小前端传的参数范围
        if (Objects.isNull(teachplanCreateCmd.getId())){
            // 创建
            Teachplan teachplan = BeanUtil.copyProperties(teachplanCreateCmd, Teachplan.class);
            teachplanService.saveTeachPlan(teachplan);
        } else {
            // 修改
            Teachplan teachplan = teachplanService.getById(teachplanCreateCmd.getId());
            BeanUtils.copyProperties(teachplanCreateCmd, teachplan);
            teachplanService.updateById(teachplan);
        }
    }

    @Override
    @Transactional
    public void moveDownTeachPlan(Long teachplanId) {
        Teachplan teachplan = teachplanService.getById(teachplanId);
        Integer thisTeachPlainOrderBy = teachplan.getOrderby();
        // 查询最高的
        Integer maxOrderBy = teachplanService.queryMaxOrderBy(teachplan);
        if (!thisTeachPlainOrderBy.equals(maxOrderBy)){
            teachplanService.updateDownOrderBy(teachplan);
        }
    }

    @Override
    public void moveUpTeachPlan(Long teachplanId) {
        Teachplan teachplan = teachplanService.getById(teachplanId);
        Integer thisTeachPlainOrderBy = teachplan.getOrderby();
        // 查询最小的
        Integer minOrderBy = teachplanService.queryMinOrderBy(teachplan);
        if (!thisTeachPlainOrderBy.equals(minOrderBy)){
            teachplanService.updateUpOrderBy(teachplan);
        }
    }

    @Override
    @Transactional
    public void deleteTeachPlan(Long teachplanId) {
        Teachplan teachplan = teachplanService.getById(teachplanId);
        List<Long> teachPlanIds = teachplanService.queryNextIds(teachplan);
        // 批量更新顺序在改课程计划之后的
        teachplanService.batchUpdateOrderBy(teachPlanIds);
        // 删除该课程计划
        teachplanService.removeById(teachplanId);
    }

    @Override
    public void associationMedia(CourseAssociationMediaCmd params) {
        Teachplan teachplan = teachplanService.getById(params.getTeachplanId());
        if (Objects.isNull(teachplan)){
            throw new XueChengPlusException("课程计划不存在");
        } else if (teachplan.getGrade() != 2){
            throw new XueChengPlusException("只有视频资源才能绑定媒资");
        }

        // 删除之前的
        teachplanMediaService.remove(new LambdaQueryWrapper<TeachplanMedia>()
                .eq(TeachplanMedia::getTeachplanId, teachplan.getId()));
        teachplanMediaService.save(TeachplanMedia.getDefault(params.getMediaId(), params.getFileName(),
                params.getTeachplanId(), teachplan.getCourseId()));
    }

    @Override
    public void deleteAssociationMedia(Long teachplanId, String mediaId) {
        teachplanMediaService.remove(new LambdaQueryWrapper<TeachplanMedia>()
                .eq(TeachplanMedia::getTeachplanId, teachplanId)
                .eq(TeachplanMedia::getMediaId, mediaId));
    }
}
